//
//  SettingsViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/2/27.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "SettingsViewController.h"
#import "FeedbackViewController.h"
#import "HelpViewController.h"
#import "AboutViewController.h"
#import <SDWebImage/SDImageCache.h>
#import "DataManager.h"
#import "LoginViewController.h"

#define kShared [DataManager defaultManager]

@interface SettingsViewController ()
@property (nonatomic,strong) NSArray *arr;
@property (nonatomic,strong) NSArray *array;
@property (nonatomic,strong) NSArray *imageArray;
@end

@implementation SettingsViewController

//图标数组的初始化
- (NSArray *)imageArray
{
    if(_imageArray == nil)
    {
        _imageArray = [NSArray arrayWithObjects:@[@"yijianfankui",@"wenhao",@"qingchuyuhuancun"],@[@"guanyu",@"kefudianhua"],nil];
    }
    return _imageArray;
}
//懒加载
- (NSArray *)array
{
    if(_array == nil)
    {
        _array = [NSArray arrayWithObjects:@[@"意见反馈",@"帮助中心",@"清除缓存"],@[@"关于掌上医院",@"拨打客服电话"], nil];
    }
    return _array;
}

//懒加载
- (NSArray *)arr
{
    if(_arr == nil)
    {
        _arr = [NSArray arrayWithObjects:@"意见反馈",@"帮助中心",@"清除缓存",@"关于掌上医院",@"拨打客服电话", nil];
    }
    return _arr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    self.navigationItem.title = @"设置";
    self.tabBarController.tabBar.hidden = YES;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.array[section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    cell.textLabel.text = self.array[indexPath.section][indexPath.row];
    cell.imageView.image = [UIImage imageNamed:self.imageArray[indexPath.section][indexPath.row]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取到当前点击的下标
    NSString *str = self.array[indexPath.section][indexPath.row];
    NSInteger index = [self.arr indexOfObject:str];
    [self pushViewControllerByIndex:index];
}

//通过下标来跳转到指定页面
- (void)pushViewControllerByIndex:(NSInteger)index
{
    switch (index) {
        case 0:
        {   //反馈信息页
            FeedbackViewController *feedbackVC = [[FeedbackViewController alloc] init];
            [self.navigationController pushViewController:feedbackVC animated:YES];
        }break;
        case 1:
        {   //帮助中心页
            HelpViewController *helpVC = [[HelpViewController alloc] init];
            [self.navigationController pushViewController:helpVC animated:YES];
        }break;
        case 2:
        {   //清除缓存
            [self clearCache];
        }break;
        case 3:
        {   //关于掌上医院
            AboutViewController *aboutVC = [[AboutViewController alloc] init];
            [self.navigationController pushViewController:aboutVC animated:YES];
        }break;
        case 4:
        {
            //打电话给客服
            UIApplication *app = [UIApplication sharedApplication];
            NSString *strUrl = [NSString stringWithFormat:@"tel://18710120875"];
            NSURL *url = [NSURL URLWithString:strUrl];
            [self showAlertTwoButtonWithTitle:@"提示" AndMessage:@"打电话给掌上医院客服" AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"拨打" AndTwoButtonTitle:@"取消" AndOneBlock:^{
                [app openURL:url ];
            } AndTwoBlock:nil];
        }break;
    }
}

//清除缓存方法
- (void)clearCache
{
    //获取缓存目录
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //得到缓存文件的大小
    CGFloat cache = [SettingsViewController folderSizeAtPath:cachesPath];
    NSString *cacheStr = [NSString stringWithFormat:@"缓存一共%0.2fM,是否清除",cache];
    [self showAlertTwoButtonWithTitle:@"提示" AndMessage:cacheStr AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"确定" AndTwoButtonTitle:@"取消" AndOneBlock:^{
        [SettingsViewController clearCache:cachesPath];
    } AndTwoBlock:nil];
}

//计算单个文件大小
+(float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}

//计算目录大小
+(float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            folderSize +=[SettingsViewController fileSizeAtPath:absolutePath];
        }
        //SDWebImage框架自身计算缓存的实现
        folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        return folderSize;
    }
    return 0;
}

//清理缓存文件
+(void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
